package edu.gduf.service.impl;

import com.fasterxml.jackson.databind.JsonNode;
import edu.gduf.dao.UserDao;
import edu.gduf.domain.Associations;
import edu.gduf.domain.LikeLog;
import edu.gduf.domain.ResultInfo;
import edu.gduf.domain.User;
import edu.gduf.service.UserService;
import edu.gduf.utils.JwxtUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.net.URISyntaxException;

@Service
public class UserServiceImpl implements UserService {

    @Autowired
    private UserDao userDao;

    @Autowired
    private JwxtUtil jwxtUtil;

    /**
     * 用户注册的方法
     * @param user
     * @return
     */
    @Override
    @Transactional
    public ResultInfo register(User user) {
        Integer res = userDao.addUser(user);
        if(user.getType() == 2) {
            Integer res2 = userDao.addAssociation(user);
            if(res == 1 && res2 == 1)
                return ResultInfo.successInfo("注册成功");
            else
                return ResultInfo.failInfo("注册失败");
        }
        if(res == 1)
            return ResultInfo.successInfo("注册成功");
        else
            return ResultInfo.failInfo("注册失败");
    }

    /**
     * 该方法通过验证用户教务系统账号和密码获取用户信息，然后封装到User类
     * @param xh
     * @param pwd
     * @return
     * @throws URISyntaxException
     */
    @Override
    public User getStudentInfo(String xh, String pwd) throws URISyntaxException {
        JsonNode studentInfo = jwxtUtil.getStudentInfo(xh, pwd);
        if(studentInfo.get("token") != null){
            return null;
        }else{
            String realName = studentInfo.get("xm").toString().replace("\"","");
            String major = studentInfo.get("zymc").toString().replace("\"","");
            String department = studentInfo.get("yxmc").toString().replace("\"","");
            String nj = studentInfo.get("nj").toString().replace("\"","").substring(2,4);
            String xb = studentInfo.get("xb").toString().replace("\"","");
            major = nj+"级"+ major;
            User user = new User();
            user.setRealname(realName);
            user.setMajor(major);
            user.setDepartment(department);
            user.setNumber(xh);
            if (xb.equals("男"))
                user.setSex(1);
            else
                user.setSex(2);
            return user;
        }
    }

    @Override
    public ResultInfo login(User user) {
        return null;
    }

    /**
     * 通过用户名获取用户信息(用于登陆）
     * @param username
     * @return
     */
    @Override
    public User findUserByUserName(String username) {
        return userDao.findUserByUsername(username);
    }

    @Override
    public String checkUserName(String username) {
        return userDao.checkUserName(username);
    }

    @Override
    public String checkXh(String xh) {
        return userDao.checkXh(xh);
    }

    @Override
    public Associations findAssociationByUid(Integer uid) {
        return userDao.findAssociationByUid(uid);
    }


    /**
     * 使用uid查找用户
     * @param uid
     * @return
     */
    @Override
    public User findUserByUid(Integer uid) {
        try{
            User user = userDao.findUserByUid(uid);
            if(user.getType() == 2){
                    user.setAssociation(userDao.findAssociationByUid(uid));
            }
            return user;
        }catch (NullPointerException e){
            System.out.println(uid+"不存在");
            return null;
        }
    }

    @Override
    public String findNickNameByUid(Integer uid) {
        return userDao.findNicknameByUid(uid);
    }

    /**
     * 用户用户的资料更新
     * @param user
     * @return 有多少数据被改变
     */
    @Override
    public Integer updateUser(User user) {
        Integer res = userDao.updateUser(user);
        if(user.getType() == 2 && user.getAssociation() != null) {
            user.getAssociation().setUid(user.getUid());
            if((user.getAssociation().getWechat() !=null && user.getAssociation().getWechat() != "")||
                    (user.getAssociation().getQqgroup()!=null && user.getAssociation().getQqgroup()!=""))
                res += userDao.updateAssociation(user.getAssociation());
        }
        return res;
    }

    /**
     * 用户修改用户的全部资料
     * @param user
     * @return
     */
    @Override
    public Integer updateUserAll(User user) {
        return userDao.updateUserAll(user);
    }


    /**
     * 用户头像更新
     * @param user
     * @return
     */
    @Override
    public ResultInfo updatePhoto(User user) {
        Integer res = userDao.updateUser(user);
        if(res >= 1){
            return ResultInfo.successInfo("上传成功");
        }else
            return ResultInfo.failInfo("上传失败");
    }

    /**
     * 对用户进行点赞，并且添加点赞记录
     * @param uid
     * @param friendid
     * @return
     */
    @Transactional
    @Override
    public Integer like(Integer uid,Integer friendid) {
        Integer like = userDao.like(friendid);
        LikeLog likeLog = new LikeLog();
        likeLog.setUid(uid);
        likeLog.setLiked_uid(friendid);
        likeLog.setTime(System.currentTimeMillis()/1000L);
        userDao.addLikeLog(likeLog);
        return like;
    }
}
